import React, { lazy, Suspense } from "react";
import { Spin } from "antd";
import {
	HomeOutlined,
	UserOutlined,
	DatabaseOutlined,
	ShopOutlined,
	UnorderedListOutlined,
	TableOutlined,
} from "@ant-design/icons";
import type { XRoutes } from "./types";

import Layouts from "../layouts";
import Redirect from "@comps/Redirect";

const Login = lazy(() => import("@pages/login"));
const Dashboard = lazy(() => import("@pages/dashboard"));
const NotFound = lazy(() => import("@pages/404"));

const Dict = lazy(() => import("@pages/cmn/dict"));
const HospitalSet = lazy(() => import("@pages/hosp/hospitalSet"));
const AddOrUpdateHospital = lazy(
	() => import("@pages/hosp/hospitalSet/components/AddOrUpdateHospital")
);
const Hospital = lazy(() => import("@pages/hosp/hospital"));
const HospitalShow = lazy(
	() => import("@pages/hosp/hospital/components/HospitalShow")
);
const HospitalSchedule = lazy(
	() => import("@pages/hosp/hospital/components/HospitalSchedule")
);
const UserList = lazy(() => import("@pages/user/userList"));
const UserListShow = lazy(
	() => import("@pages/user/userList/components/UserListShow")
);
const AuthList = lazy(() => import("@pages/user/authList"));
const OrderList = lazy(() => import("@pages/order/orderList"));
const OrderListShow = lazy(
	() => import("@pages/order/orderList/components/OrderListShow")
);
const StatisticsOrder = lazy(() => import("@pages/statistics/order"));

export const asyncRoutes: XRoutes = [
	{
		path: "/",
		meta: { icon: <HomeOutlined />, title: "首页" },
		element: <Redirect from="/" to="/dashboard" />,
		children: [
			{
				path: "dashboard",
				index: true,
				meta: { icon: <HomeOutlined />, title: "Dashboard" },
				element: (
					<Suspense fallback={<Spin />}>
						<Dashboard />
					</Suspense>
				),
			},
		],
	},
	{
		path: "/cmn",
		meta: { icon: <DatabaseOutlined />, title: "数据管理" },
		element: <Redirect from="/cmn" to="/cmn/dict/list" />,
		children: [
			{
				path: "dict/list",
				element: (
					<Suspense fallback={<Spin />}>
						<Dict />
					</Suspense>
				),
				meta: { title: "数据字典" },
			},
		],
	},
	{
		path: "/hosp",
		meta: { icon: <ShopOutlined />, title: "医院管理" },
		element: <Redirect from="/hosp" to="/hosp/hospitalSet/list" />,
		children: [
			{
				path: "hospitalSet/list",
				element: (
					<Suspense fallback={<Spin />}>
						<HospitalSet />
					</Suspense>
				),
				meta: { title: "医院设置" },
			},
			{
				path: "hospitalSet/add",
				element: (
					<Suspense fallback={<Spin />}>
						<AddOrUpdateHospital />
					</Suspense>
				),
				meta: { title: "医院设置" },
				hidden: true,
			},
			{
				path: "hospitalSet/edit/:id",
				element: (
					<Suspense fallback={<Spin />}>
						<AddOrUpdateHospital />
					</Suspense>
				),
				meta: { title: "医院设置" },
				hidden: true,
			},
			{
				path: "hospital/list",
				element: (
					<Suspense fallback={<Spin />}>
						<Hospital />
					</Suspense>
				),
				meta: { title: "医院列表" },
			},
			{
				path: "hospital/show/:id",
				element: (
					<Suspense fallback={<Spin />}>
						<HospitalShow />
					</Suspense>
				),
				meta: { title: "医院列表" },
				hidden: true,
			},
			{
				path: "hospital/schedule/:hoscode",
				element: (
					<Suspense fallback={<Spin />}>
						<HospitalSchedule />
					</Suspense>
				),
				meta: { title: "医院列表" },
				hidden: true,
			},
		],
	},
	{
		path: "/user",
		meta: { icon: <UserOutlined />, title: "会员管理" },
		element: <Redirect from="/user" to="/user/userInfo/list" />,
		children: [
			{
				path: "userInfo/list",
				element: (
					<Suspense fallback={<Spin />}>
						<UserList />
					</Suspense>
				),
				meta: { title: "会员列表" },
			},
			{
				path: "userInfo/show/:id",
				element: (
					<Suspense fallback={<Spin />}>
						<UserListShow />
					</Suspense>
				),
				meta: { title: "会员列表" },
				hidden: true,
			},
			{
				path: "userInfo/authList",
				element: (
					<Suspense fallback={<Spin />}>
						<AuthList />
					</Suspense>
				),
				meta: { title: "认证审批列表" },
			},
		],
	},
	{
		path: "/order",
		meta: { icon: <UnorderedListOutlined />, title: "订单管理" },
		element: <Redirect from="/order" to="/order/orderInfo/list" />,
		children: [
			{
				path: "orderInfo/list",
				meta: { title: "订单列表" },
				element: (
					<Suspense fallback={<Spin />}>
						<OrderList />
					</Suspense>
				),
			},
			{
				path: "orderInfo/show/:id",
				element: (
					<Suspense fallback={<Spin />}>
						<OrderListShow />
					</Suspense>
				),
				meta: { title: "订单列表" },
				hidden: true,
			},
		],
	},
	{
		path: "/statistics",
		meta: { icon: <TableOutlined />, title: "统计管理" },
		element: <Redirect from="/statistics" to="/statistics/order/list" />,
		children: [
			{
				path: "order/list",
				meta: { title: "预约统计" },
				element: (
					<Suspense fallback={<Spin />}>
						<StatisticsOrder />
					</Suspense>
				),
			},
		],
	},
];

export const constantRoutes: XRoutes = [
	{
		path: "/login",
		element: (
			<Suspense fallback={<Spin />}>
				<Login />
			</Suspense>
		),
	},
	{
		path: "/404",
		element: (
			<Suspense fallback={<Spin />}>
				<NotFound />
			</Suspense>
		),
	},
	{
		path: "/",
		element: <Layouts />,
		children: asyncRoutes,
	},
	{
		path: "*",
		element: <Redirect to="/404" />,
	},
];

// export const routes = asyncRoutes.reduce<XRoutes>((prev, cur) => {
// 	if (cur.children) {
// 		const children = cur.children.map((item) => {
// 			return {
// 				path: cur.path + item.path,
// 				component: item.component,
// 			};
// 		});
// 		prev = prev.concat(children);
// 	} else {
// 		prev.push(cur);
// 	}
// 	return prev;
// }, []);
